package com.mylove.judas.yaya.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.NativeExpressAdView;
import com.google.android.gms.ads.formats.NativeAd;
import com.google.android.gms.ads.formats.NativeAppInstallAd;
import com.google.android.gms.ads.formats.NativeAppInstallAdView;
import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.common.utils.DensityUtil;
import com.mylove.judas.yaya.entity.BaseEntity;
import com.mylove.judas.yaya.ui.activity.BigImageActivity;
import com.widget.dialog.ShowImagesDialog;

import java.util.List;

import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by Judas on 2018/4/26.
 */

public class ListAdapter extends RecyclerView.Adapter {

    private static final int TYPE_TEXT = 1;
    private static final int TYPE_IMAGE = 2;
    private static final int TYPE_GIF = 3;
    private static final int TYPE_VIDEO = 4;
    private static final int TYPE_AD = 5;

    private Context mContext;
    private List<BaseEntity> list;

    public ListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<BaseEntity> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = null;

        switch (viewType){
            case TYPE_TEXT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_text, parent, false);

                return new TextViewHolder(view);
            case TYPE_IMAGE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_image, parent, false);

                return new ImageViewHolder(view);
            case TYPE_GIF:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_image, parent, false);

                return new GIFViewHolder(view);
            case TYPE_VIDEO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_video, parent, false);

                return new VideoViewHolder(view);
            default:

                //TODO 加载原生广告，暂时无效
//                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_install_ad, parent, false);

                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_ad, parent, false);

                break;
        }

        return new AdViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

//        int type = getItemViewType(position);

        final BaseEntity item = list.get(position);


        if(holder instanceof VideoViewHolder){
            VideoViewHolder vvh = (VideoViewHolder) holder;

            vvh.loadData(item);

//            vvh.jzVideoPlayerStandard.setUp(item.getVideouri(), JZVideoPlayerStandard.SCREEN_WINDOW_LIST, item.getText());
            vvh.jzVideoPlayerStandard.setUp(item.getVideo(), JZVideoPlayerStandard.SCREEN_WINDOW_LIST, "");
//            vvh.jzVideoPlayerStandard.thumbImageView.setImageURI(Uri.parse());
            Glide.with(mContext).load(item.getThumbnail()).into(vvh.jzVideoPlayerStandard.thumbImageView);

            vvh.jzVideoPlayerStandard.positionInList = position;


//            //====
//            vvh.videoPlayerView.bind(item.getVideouri(), item.getText());
//            Glide.with(mContext).load(item.getImage1()).into(vvh.videoPlayerView.getThumbImageView());
        }else if(holder instanceof ImageViewHolder){

            ImageViewHolder ivh = (ImageViewHolder) holder;

            ivh.loadData(item);

            final String url = item.getImage();

            Glide.with(mContext).load(url)
//                  .override(width, height)
                    .placeholder(R.drawable.ic_image_default_white_24dp)
                    .error(R.drawable.ic_image_error_white_24dp)
                    .crossFade()
                    .centerCrop()
                    .into(ivh.iv_image);

            setImageViewHeight(ivh.iv_image);

            ivh.iv_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                  new ShowImagesDialog(mContext, item.getImage1()).show();
                    Intent intent = new Intent(mContext, BigImageActivity.class);
                    intent.putExtra("URL", url);
                    mContext.startActivity(intent);
                    }
                });


//            Glide.with(mContext).load(item.getImage1()).placeholder(R.mipmap.ic_launcher).thumbnail(0.1f).into(ivh.iv_image);

//            ivh.iv_image.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    new ShowImagesDialog(mContext, item.getImage1()).show();
//                    Intent intent = new Intent(mContext, BigImageActivity.class);
//                    intent.putExtra("URL", url);
//                    mContext.startActivity(intent);
//                }
//            });

        }else if(holder instanceof GIFViewHolder){

            GIFViewHolder gvh = (GIFViewHolder) holder;

            gvh.loadData(item);

            final String url = item.getImage();
            Glide.with(mContext).load(url).asGif()
//                        .override(width, height)
                    .placeholder(R.drawable.ic_image_default_white_24dp)
                    .error(R.drawable.ic_image_error_white_24dp)
                    .dontAnimate()//去掉显示动画
//                        .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)//DiskCacheStrategy.NONE
                    .into(gvh.iv_image);

            setImageViewHeight(gvh.iv_image);

            gvh.iv_image.setOnClickListener(null);

        }else if(holder instanceof TextViewHolder){

            TextViewHolder tvh = (TextViewHolder) holder;

            tvh.loadData(item);

        }else if(holder instanceof AdViewHolder){

            final AdViewHolder avh = (AdViewHolder) holder;

            avh.loadAd();

        }

    }

    /**
     * 按照图片比例等比缩放ImageView控件的高度
     * @param iv
     */
    public void setImageViewHeight(ImageView iv){

        //获取原始ImageView的宽度
        ViewGroup.LayoutParams params = iv.getLayoutParams();

        params.height = params.width;

        iv.setLayoutParams(params);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {

        BaseEntity item = list.get(position);
//        type=10 图片， type=41 MP4 type=29 文字
//        type=text : 文字
//        type=image : 图片
//        type=gif : Gif
//        type=video: 视频
        if(item.getType().equals("10") || item.getType().equals("image")){
            return TYPE_IMAGE;
        }else if(item.getType().equals("41") || item.getType().equals("video")){
            return TYPE_VIDEO;
        }else if(item.getType().equals("29") || item.getType().equals("text")){
            return TYPE_TEXT;
        }else if(item.getType().equals("gif")){
            return TYPE_GIF;
        }

        return TYPE_AD;
    }

    /**
     * 文本
     */
    class TextViewHolder extends RecyclerView.ViewHolder{

        public ImageView iv_header;
        public TextView tv_author;
        public TextView tv_content;

        public TextViewHolder(View itemView) {
            super(itemView);

            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);

        }

        /**
         * 加载通用数据
         * @param item
         */
        public void loadData(BaseEntity item){

            tv_author.setText(item.getUsername());
            tv_content.setText(item.getText());

            Glide.with(mContext).load(item.getHeader()).into(iv_header);
        }

    }

    /**
     * 图片
     */
    class ImageViewHolder extends TextViewHolder{

        public ImageView iv_image;

        public ImageViewHolder(View itemView) {
            super(itemView);

            iv_image = (ImageView) itemView.findViewById(R.id.iv_image);

        }

    }

    /**
     * 图片
     */
    class GIFViewHolder extends TextViewHolder{

        public ImageView iv_image;

        public GIFViewHolder(View itemView) {
            super(itemView);

            iv_image = (ImageView) itemView.findViewById(R.id.iv_image);

        }

    }

    /**
     * 视频Video
     */
    class VideoViewHolder extends TextViewHolder{

        public JZVideoPlayerStandard jzVideoPlayerStandard;

//        public VideoPlayerView videoPlayerView;

        public VideoViewHolder(View itemView) {
            super(itemView);

            jzVideoPlayerStandard = (JZVideoPlayerStandard) itemView.findViewById(R.id.video);

//            videoPlayerView = (VideoPlayerView) itemView.findViewById(R.id.video_player_view);
        }

    }

    /**
     * adview
     */
    class AdViewHolder extends RecyclerView.ViewHolder{

        /**
         * TODO 加载原生广告，暂时无效
         */
//        private NativeAppInstallAdView adView;
//        private TextView contentExtraTextView;
//
//        private String extraText="SampleId App-Install";

        private AdView adview;

        public AdViewHolder(View itemView) {
            super(itemView);

            /**
             * TODO 加载原生广告， 暂时无效
             */
//            adView = (NativeAppInstallAdView) itemView;
//
//            adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
//            adView.setImageView(adView.findViewById(R.id.appinstall_image));
//            adView.setBodyView(adView.findViewById(R.id.appinstall_body));
//            adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
//            adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
//            adView.setPriceView(adView.findViewById(R.id.appinstall_price));
//            adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
//            adView.setStoreView(adView.findViewById(R.id.appinstall_store));
//            contentExtraTextView= (TextView) adView.findViewById(R.id.appinstall_advertisement);


            adview = (AdView) itemView.findViewById(R.id.adView);
        }

        /**
         * TODO 加载原生广告， 暂时无效
         * @param appInstallAd
         */
        public void populateView(NativeAppInstallAd appInstallAd) {
//            ((TextView) adView.getHeadlineView()).setText(appInstallAd.getHeadline());
//            ((TextView) adView.getBodyView()).setText(appInstallAd.getBody());
//            ((TextView) adView.getPriceView()).setText(appInstallAd.getPrice());
//            ((TextView) adView.getStoreView()).setText(appInstallAd.getStore());
//            ((Button) adView.getCallToActionView()).setText(appInstallAd.getCallToAction());
//            ((ImageView) adView.getIconView()).setImageDrawable(appInstallAd.getIcon().getDrawable());
//            ((RatingBar) adView.getStarRatingView())
//                    .setRating(appInstallAd.getStarRating().floatValue());
//            contentExtraTextView.setText("\n"+extraText);
//            List<NativeAd.Image> images = appInstallAd.getImages();
//
//            if (images.size() > 0) {
//                ((ImageView) adView.getImageView())
//                        .setImageDrawable(images.get(0).getDrawable());
//            }
//
//            // assign native ad object to the native view and make visible
//            adView.setNativeAd(appInstallAd);
//            adView.setVisibility(View.VISIBLE);
        }


        public void loadAd(){

            adview.loadAd(new AdRequest.Builder()
//                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build());

        }
    }

}
